package com.infosys.cabplanning.service.geolocator;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.infosys.cabplanning.common.domain.GeoCoordidnate;
import com.infosys.cabplanning.common.enums.LocationSortEnum;
import com.infosys.cabplanning.common.util.GenericConstants;

public class LocationSorter implements Comparator<GeoCoordidnate>{

	private List<GeoCoordidnate> geoCoordinateList;
	private String sortParam;
	private String sortOrder;

	public List<GeoCoordidnate> getGeoCoordinateList() {
		return geoCoordinateList;
	}

	public void setGeoCoordinateList(List<GeoCoordidnate> geoCoordinateList) {
		this.geoCoordinateList = geoCoordinateList;
	}

	public String getSortParam() {
		return sortParam;
	}

	public void setSortParam(String sortParam) {
		this.sortParam = sortParam;
	}

	public String getSortOrder() {
		return sortOrder;
	}

	public void setSortOrder(String sortOrder) {
		this.sortOrder = sortOrder;
	}

	public LocationSorter(List<GeoCoordidnate> geoCoordinateList, String sortParam, String sortOrder) {
		this.geoCoordinateList = geoCoordinateList;
		this.sortParam = sortParam;
		this.sortOrder = sortOrder;
	}

	/* (non-Javadoc)
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	@Override
	public int compare(GeoCoordidnate firstObject, GeoCoordidnate secondObject) {

		int result = 0;
//		List<GeoCoordidnateDomain> sortedList = null;
		LocationSortEnum sortParam = LocationSortEnum.fromValue(this.getSortParam());
		switch (sortParam) {
		
		case latitude:
			if (sortOrder.equalsIgnoreCase(GenericConstants.SORT_ASCENDING)) {
				result = firstObject.getLatitude().compareTo(
						secondObject.getLatitude());
			} else {
				result = secondObject.getLatitude().compareTo(
						firstObject.getLatitude());
			}
			
			break;
		
		case longitude:
			if (sortOrder.equalsIgnoreCase(GenericConstants.SORT_ASCENDING)) {
				result = firstObject.getLongitude().compareTo(
						secondObject.getLongitude());
			} else {
				result = secondObject.getLongitude().compareTo(
						firstObject.getLongitude());
			}
			
			break;

		default:
			System.out.println("do nothing");
		
		}

		return result;
	}
	
	
	public List<GeoCoordidnate> sortLocations() {
		
		Collections.sort(getGeoCoordinateList(), this);
		
		return getGeoCoordinateList();
	}
	
	public static void main(String[] args) {

	}
}
